package com.example.websocket_demo.controller.user;

import com.example.websocket_demo.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("signUp")
    public Map<String, Object> signUp(@RequestBody Map<String, Object> userMap) {
        return userService.signUp(userMap);
    }
    @PostMapping("signIn")
    public Map<String, Object> signIn(@RequestBody Map<String, Object> userMap,HttpSession session) {
        System.out.println(session.getId());
        Map<String, Object> res=userService.signIn(userMap);
        res.put("sessionId",session.getId());
        session.setAttribute("userInfo",res);
        return res;
    }
    @PostMapping("getUserInfo")
    public Map<String, Object> getUserInfo(@RequestBody Map<String, Object> userMap,HttpSession session) {
        System.out.println(session.getId());
        return (Map<String, Object>)session.getAttribute("userInfo");
    }

    @PostMapping("selectUser")
    public List<Map<String,Object>> selectUser(@RequestBody Map<String, Object> userMap) {
        return userService.selectUser(userMap);
    }

    @PostMapping("delete")
    public int deleteUser(@RequestBody Map<String, Object> userMap) {
        return userService.deleteUser((Integer) userMap.get("id"));
    }
    /**
     * 1.@RequestPart这个注解用在multipart/form-data表单提交请求的方法上。
     * 2.支持的请求方法的方式MultipartFile，属于Spring的MultipartResolver类。这个请求是通过http协议传输的。
     * 3.@RequestParam也同样支持multipart/form-data请求。
     * 4.他们最大的不同是，当请求方法的请求参数类型不再是String类型的时候。
     * 5.@RequestParam适用于name-valueString类型的请求域，@RequestPart适用于复杂的请求域（像JSON，XML,当然前端需要对应的格式的Blob对象）。
     */
    @PostMapping(value = "update")
    public int updateUser(@RequestPart(value = "userMap",required = false) Map<String, Object> userMap, @RequestParam(value = "file",required = false) MultipartFile file) {
//        return userService.updateUser(userMap,file);
        return 0;
    }

    @GetMapping("/user")
    public List<Map<String, Object>> selectUsers() {
        return userService.selectUsers();
    }
}
